package com.woniuxy.service.impl;

import com.woniuxy.pojo.Department;
import com.woniuxy.mapper.DepartmentMapper;
import com.woniuxy.pojo.RespBean;
import com.woniuxy.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuankaiqiang
 * @since 2022-01-28
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired(required = false)
    private DepartmentMapper departmentMapper;
    /**
     * Description:获取所有部门
     * @author: ykq11  <br/>
     * Date: 2022/2/15:20:51  <br/>
     */
    @Override
    public List<Department> getAllDepartment() {
        return departmentMapper.getAllDepartment(-1);
    }

    /**
     * Description : TODO  添加部门<br/>
     * ChangeLog : 1. 创建 (2022/2/15 21:57 [ykq11]);
     ** @param department
     * *  @return null
     */
    @Override
    public RespBean addDep(Department department) {
        department.setEnabled(true);
        departmentMapper.addDep(department);
        if(1==department.getResult()){
            return RespBean.success("添加成功",department);
        }
        return RespBean.error("添加失败!");
    }

    /**
     * Description : TODO  删除部门<br/>
     * ChangeLog : 1. 创建 (2022/2/15 22:18 [ykq11]);
     ** @param depId
     * *  @return null
     */
    @Override
    public RespBean deleteDep(Integer depId) {
        Department department = new Department();
        department.setId(depId);
        departmentMapper.deleteDep(department);
        if(-2==department.getResult()){
            return RespBean.error("该部门下还有子部门，删除失败");
        }
        if (-1==department.getResult()) {
            return RespBean.error("该部门下还有员工,删除失败");
        }
        if (1==department.getResult()) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }
}
